package com.example.guiguiapplicatio.adapter;

import static android.app.Activity.RESULT_OK;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.guiguiapplicatio.LocalSaveActivity;
import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.databinding.ActivityLocalSaveItemBinding;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class LocalSaveActivityAdapter extends RecyclerView.Adapter<LocalSaveActivityAdapter.ViewHolder> {

    List<String> setUpTimeList = new ArrayList<>();
    String dataType = "";
    SharedPreferences.Editor edit;
    SharedPreferences sp;
    Context context;
    int dataSize;
    Gson gson = new Gson();
    String pendingData = "";
    boolean IS_SAVE = false;

    public LocalSaveActivityAdapter(List<String> setUpTimeList, String dataType, Context context, SharedPreferences.Editor edit, SharedPreferences sp, int DATA_SIZE, String data, boolean IS_SAVE) {
        this.setUpTimeList = setUpTimeList;

        Log.e("TAG", "LocalSaveActivityAdapter: 查看数据是否加载"+setUpTimeList.size() );
        this.context = context;

        if (dataType != null) {
            this.dataType = dataType;
        }
        this.edit = edit;
        this.sp=sp;
        this.dataSize = DATA_SIZE;

        if (data.length() > 0) {
            this.pendingData = data;
        }
        this.IS_SAVE = IS_SAVE;
    }

    @NonNull
    @Override
    public LocalSaveActivityAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(ActivityLocalSaveItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull LocalSaveActivityAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        int mPosition = position;

        if (IS_SAVE) {//保存数据

            if (mPosition < setUpTimeList.size()) {//正常数据显示
                holder.binding.dataKayout.setVisibility(View.VISIBLE);
                holder.binding.addKayout.setVisibility(View.GONE);

                holder.item = gson.fromJson(setUpTimeList.get(mPosition), ItemDescription.class);

                holder.binding.time.setText(holder.item.date);
                if (holder.item.description != null) {
                    holder.binding.description.setText(holder.item.description);
                }

                holder.binding.dataKayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        coverData(mPosition);

                    }
                });
            } else {//空数据添加显示
                holder.binding.dataKayout.setVisibility(View.GONE);
                holder.binding.addKayout.setVisibility(View.VISIBLE);
                holder.binding.addKayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        addData(mPosition);

                    }
                });
            }

        } else {//数据取出

            if (mPosition < setUpTimeList.size()) {//正常数据显示
                holder.binding.dataKayout.setVisibility(View.VISIBLE);
                holder.binding.addKayout.setVisibility(View.GONE);

                holder.item = gson.fromJson(setUpTimeList.get(mPosition), ItemDescription.class);

                holder.binding.time.setText(holder.item.date);
                if (holder.item.description != null) {
                    holder.binding.description.setText(holder.item.description);
                }

                holder.binding.dataKayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        loadData(mPosition);
                    }
                });
            } else {//空数据不显示
                holder.binding.dataKayout.setVisibility(View.GONE);
                holder.binding.addKayout.setVisibility(View.GONE);
            }

        }


    }

    private void loadData(int mPosition) {

        AlertDialog aldg;
        AlertDialog.Builder adBd = new AlertDialog.Builder(context);
        adBd.setTitle("是否取出改条记录,并覆盖到随访");
        adBd.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                if (LocalSaveActivity.class.isInstance(context)){
                    LocalSaveActivity localSaveActivity = (LocalSaveActivity) context;
                    Intent intent = new Intent();
                    intent.putExtra("data_return",sp.getString(dataType + "_" + mPosition,""));
                    localSaveActivity.setResult(RESULT_OK,intent);
                    localSaveActivity.finish();

                }

            }
        });
        adBd.setNegativeButton("取消",null);
        aldg = adBd.create();
        aldg.show();

    }

    private void coverData(int mPosition) {
        AlertDialog aldg;
        AlertDialog.Builder adBd = new AlertDialog.Builder(context);
        adBd.setTitle("是否覆盖此条记录");
        adBd.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                ItemDescription itemDescription = new ItemDescription();
                itemDescription.setDate(getNowTime());
                itemDescription.setDescription("");

                final EditText editText = new EditText(context);

                AlertDialog aldg;
                AlertDialog.Builder adBd = new AlertDialog.Builder(context);
                adBd.setTitle("是否添加额外信息或描述？");
                adBd.setView(editText);
                adBd.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        itemDescription.setDescription(String.valueOf(editText.getText()));

                        edit.putString(dataType + "_DSC_" + mPosition, gson.toJson(itemDescription)).apply();
                        edit.putString(dataType + "_" + mPosition, gson.toJson(pendingData)).apply();

                        setUpTimeList.set(mPosition, gson.toJson(itemDescription));
                        notifyDataSetChanged();

                    }
                });

                adBd.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        edit.putString(dataType + "_DSC_" + mPosition, gson.toJson(itemDescription)).apply();
                        edit.putString(dataType + "_" + mPosition, gson.toJson(pendingData)).apply();

                        setUpTimeList.set(mPosition, gson.toJson(itemDescription));
                        edit.clear().apply();
                        notifyDataSetChanged();

                    }
                });
                aldg = adBd.create();
                aldg.show();


            }
        });
        adBd.setNegativeButton("取消", null);
        aldg = adBd.create();
        aldg.show();
    }

    private void addData(int mPosition) {


        AlertDialog aldg;
        AlertDialog.Builder adBd = new AlertDialog.Builder(context);
        adBd.setTitle("是否添加");
        adBd.setPositiveButton("确定", new DialogInterface.OnClickListener() {


            @Override
            public void onClick(DialogInterface dialog, int which) {

                ItemDescription itemDescription = new ItemDescription();
                itemDescription.setDate(getNowTime());
                itemDescription.setDescription("");

                final EditText editText = new EditText(context);

                AlertDialog aldg;
                AlertDialog.Builder adBd = new AlertDialog.Builder(context);
                adBd.setTitle("是否添加额外信息或描述？");
                adBd.setView(editText);
                adBd.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        itemDescription.setDescription(String.valueOf(editText.getText()));

                        edit.putString(dataType + "_DSC_" + mPosition, gson.toJson(itemDescription)).apply();
                        edit.putString(dataType + "_" + mPosition, gson.toJson(pendingData)).apply();

                        setUpTimeList.add(gson.toJson(itemDescription));
                        notifyDataSetChanged();

                    }
                });

                adBd.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        edit.putString(dataType + "_DSC_" + mPosition, gson.toJson(itemDescription)).apply();
                        edit.putString(dataType + "_" + mPosition, gson.toJson(pendingData)).apply();

                        setUpTimeList.add(gson.toJson(itemDescription));
                        notifyDataSetChanged();

                    }
                });
                aldg = adBd.create();
                aldg.show();

            }
        });
        adBd.setNegativeButton("取消", null);
        aldg = adBd.create();
        aldg.show();
    }

    private String getNowTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
        Date date = new Date(System.currentTimeMillis());
        //获取当前时间
        return simpleDateFormat.format(date);
    }

    @Override
    public int getItemCount() {
        if (setUpTimeList.size() == dataSize) {
            return setUpTimeList.size();

        } else
            return setUpTimeList.size() + 1;

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ItemDescription item;

        ActivityLocalSaveItemBinding binding;

        public ViewHolder(@NonNull ActivityLocalSaveItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    public class ItemDescription {
        String date;

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        String description;

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }
    }
}
